//
//  ST_SKDetectingImageView.swift
//  ST_ST_SKPhotoBrowser
//
//  Created by suzuki_keishi on 2015/10/01.
//  Copyright © 2015 suzuki_keishi. All rights reserved.
//

import UIKit
import MBProgressHUD
import SwifterSwift

@objc protocol ST_SKDetectingImageViewDelegate {
    func handleImageViewSingleTap(_ touchPoint: CGPoint)
    func handleImageViewDoubleTap(_ touchPoint: CGPoint)
}

class ST_SKDetectingImageView: UIImageView {
    weak var delegate: ST_SKDetectingImageViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    @objc func handle_double_tap(_ recognizer: UITapGestureRecognizer) {
        delegate?.handleImageViewDoubleTap(recognizer.location(in: self))
    }
    
    @objc func handle_single_tap(_ recognizer: UITapGestureRecognizer) {
        delegate?.handleImageViewSingleTap(recognizer.location(in: self))
    }
    
    @objc func long_pressed(recognizer: UILongPressGestureRecognizer)
    {
        
        if recognizer.state == .began {
            
            if let willSaveImage = self.image{
                
                let alert = UIAlertController.init(title: nil, message: nil, preferredStyle: UIAlertController.Style.actionSheet)
                
                alert.addAction(title: "保存图片", style: UIAlertAction.Style.default, isEnabled: true) { (action) in
                    
                    UIImageWriteToSavedPhotosAlbum(willSaveImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
                }
                
                alert.addAction(title: "取消", style: UIAlertAction.Style.cancel, isEnabled: true
                    , handler: nil)
                
                if let vc = self.parentViewController{
                    vc.present(alert, animated: true, completion: nil)
                }
                
                
            }
        }
    }
    
    //MARK: - Add image to Library
    @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        
        if let vc = self.parentViewController{

            var text = "保存失败"
            if error == nil{
                text = "成功保存到相册"
            }
            
            ToastHud(text: text, view: vc.view)
        }
    }
}

private extension ST_SKDetectingImageView {
    func setup() {
        isUserInteractionEnabled = true
        
        let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handle_double_tap(_:)))
        doubleTap.numberOfTapsRequired = 2
        addGestureRecognizer(doubleTap)
        
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(handle_single_tap(_:)))
        singleTap.require(toFail: doubleTap)
        addGestureRecognizer(singleTap)
        
        let longPressRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(long_pressed(recognizer:)))
        
        addGestureRecognizer(longPressRecognizer)
    }
}
